<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Counting_action_log extends CI_Controller
{

	public	function __construct()
	{
		parent::__construct();
		$this->load->library('pagination');
		if($this->session->userdata('user_id')){
			$this->load->helper("html");
			$this->load->model("tailieus");
			$this->load->model("giaobans");
			$this->load->model("users");
			$this->load->model("lich");
			$this->load->model("log_model");
			//date_default_timezone_set('Asia/Ho_Chi_Minh');
		}
		else{
			redirect('login');
		}
	}
	public function index($msg=null){
		$data['menu'] = 'user_'.$msg;
		$data['list_tailieu'] = $this->tailieus->listTailieu();
		//$data['list_giaoban'] = $this->giaobans->listGiaoban();
		/////////////
		$data['menu_tin'] = $this->giaobans->listGiaoban();
		$list = array();
		foreach ($data['menu_tin'] as $l){		//sap xep nhom danh sach theo giao ban cha
			if($l->id_type_giaoban != 9)
				$list[$l->cha_giaoban][] = $l;
		}
		
		$data['multi_menu_tin'] = $this->giaobans->multiGiaoban($list,0);						
		////////////
		$data['list_lich'] = $this->lich->listLich();
		$this->load->view('header_block_v1',$data);
		$this->load->view('content_sidebar_block',$data);
		switch ($msg){
			case 'log':
				$this->viewLog();
				break;
			default:
				$this->viewLog();
				break;
		}
		$this->load->view('footer_block');
	}
	private function viewLog(){
		$this->load->view("log/viewLog");
	}
	public function showLogDataTable(){
			
		$aColumns = array('date_log_vtv4', 'ten_nguoidung', 'name_action_logcol_vtv4', 'comment_log_vtv4');
		$_aColumns = array( 'date_log_vtv4', 'B.ten_nguoidung', 'C.name_action_logcol_vtv4', 'comment_log_vtv4');
		/* Indexed column (used for fast and accurate table cardinality) */
		$sIndexColumn = "id_log_vtv4";

		// DB table to use
		$sTable = 'vtv4_log';

		$iDisplayStart = $this->input->get_post('iDisplayStart', true);
		$iDisplayLength = $this->input->get_post('iDisplayLength', true);
		$iSortCol_0 = $this->input->get_post('iSortCol_0', true);
		$iSortingCols = $this->input->get_post('iSortingCols', true);
		$sSearch = $this->input->get_post('sSearch', true);
		$sEcho = $this->input->get_post('sEcho', true);

		// Paging
		if(isset($iDisplayStart) && $iDisplayLength != '-1' && $iDisplayLength != '0')
		{
			$this->db->limit($this->db->escape_str($iDisplayLength), $this->db->escape_str($iDisplayStart));
		}
		// Ordering
		if(isset($iSortCol_0))
		{
			for($i=0; $i<intval($iSortingCols); $i++)
			{
				$iSortCol = $this->input->get_post('iSortCol_'.$i, true);
				$bSortable = $this->input->get_post('bSortable_'.intval($iSortCol), true);
				$sSortDir = $this->input->get_post('sSortDir_'.$i, true);

				if($bSortable == 'true')
				{
					$this->db->order_by($_aColumns[intval($this->db->escape_str($iSortCol))], $this->db->escape_str($sSortDir));
				}
			}
		}

		if(isset($sSearch) && !empty($sSearch))
		{
			for($i=0; $i<count($_aColumns); $i++)
			{
				$bSearchable = $this->input->get_post('bSearchable_'.$i, true);

				// Individual column filtering
				if(isset($bSearchable) && $bSearchable == 'true')
				{
					$this->db->or_like($_aColumns[$i], $this->db->escape_like_str($sSearch));
				}
			}
		}
		// Select Data
		$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $_aColumns)), false);
		$this->db->from('vtv4_log A');
		$this->db->join('vtv4_nguoidung B', 'B.id_nguoidung = A.id_nguoidung_vtv4_pk');
		$this->db->join('vtv4_list_action_log C', 'C.id_list_action_log_vtv4 = A.action_log_vtv4');
		$rResult = $this->db->get();
		//$rResult = $this->db->get($sTable);
		// Data set length after filtering
		$this->db->select('FOUND_ROWS() AS found_rows');
		$iFilteredTotal = $this->db->get()->row()->found_rows;
		// Total data set length
		$iTotal = $this->db->count_all($sTable);
		// Output
		$output = array(
				'sEcho' => intval($sEcho),
				'iTotalRecords' => $iTotal,
				'iTotalDisplayRecords' => $iFilteredTotal,
				'aaData' => array()
		);

		$result = $rResult->result_array();
		//print_r($result);
		foreach($result as $log){
			$row = array();
			for ( $i=0 ; $i<count($aColumns) ; $i++ ){
				$row[] = $log[$aColumns[$i]];				
			}
			$output['aaData'][] = $row;
		}
		/*echo "<pre>";
		 print_r($output);
		echo "</pre>";*/
		echo json_encode( $output );
	}

}